package com.xgry.bpm.api.base.formlist.service.impl;

import com.xgry.bpm.api.base.formlist.entity.FormList;
import com.xgry.bpm.api.base.formlist.mapper.FormListMapper;
import com.xgry.bpm.api.base.formlist.service.FormListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.apache.http.HttpStatus;

import java.util.List;

/**
* <p>
    * FormList服务实现类
    * </p>
*
* @author adrian
* @since 2020-07-07
*/
@Service
public class FormListServiceImpl extends ServiceImpl<FormListMapper, FormList>implements FormListService {

    @Override
    public int deleteById(String id){
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException(String.valueOf(HttpStatus.SC_METHOD_FAILURE));
        }
        return baseMapper.deleteById(id);
    }

    @Override
    public FormList selectById(String id){
        if(StringUtils.isEmpty(id)){
            throw new IllegalArgumentException(String.valueOf(HttpStatus.SC_METHOD_FAILURE));
        }
        FormList data=baseMapper.selectById(id);
        return data;
    }

    @Override
    public List<FormList>selectAll(FormList record){
        QueryWrapper<FormList>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(record);
        List<FormList>list=baseMapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public IPage<FormList>selectPage(IPage<FormList> page , FormList record){
        // 查询条件
        QueryWrapper<FormList>queryWrapper=new QueryWrapper<>();
        queryWrapper.setEntity(record);
        return baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public IPage<FormList>getPage(IPage<FormList>page,QueryWrapper<FormList>queryWrapper){
        return baseMapper.selectPage(page,queryWrapper);
    }

    @Override
    public FormList findByProcessInstanceId(String processDefinitionId) {
        List<FormList> formLists = baseMapper.selectList(new QueryWrapper<FormList>() {{
            this.eq("processInstanceId", processDefinitionId);
        }});
        if (CollectionUtils.isEmpty(formLists)) {
            return null;
        }else{
            return formLists.get(0);
        }
    }

    @Override
    public FormList findByCtableName(String cTableName) {
        List<FormList> formLists = baseMapper.selectList(new QueryWrapper<FormList>() {{
            this.eq("cTableName", cTableName);
        }});
        return CollectionUtils.isEmpty(formLists)?null:formLists.get(0);
    }

    @Override
    public List<FormList> findByModelId(Integer modelId) {
        return  baseMapper.selectList(new QueryWrapper<FormList>() {{
            this.eq("modelId", modelId);
        }});
    }
}